home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / internet / other / mail / pathalia.zoo / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-12  |  3.9 KB  |  161 lines

  1. /* pathalias -- by steve bellovin, as told to peter honeyman */
  2. #ifndef lint
  3. static char    *sccsid = "@(#)main.c    9.5 88/06/10";
  4. #endif
  5.  
  6. #define MAIN    /* for sccsid in header files */
  7.  
  8. #include "def.h"
  9.  
  10. /* exports */
  11. char *Cfile;    /* current input file */
  12. char *Graphout;    /* file for dumping edges (-g option) */
  13. char *Linkout;    /* file for dumping shortest path tree */
  14. char **Argv;    /* external copy of argv (for input files) */
  15. node *Home;    /* node for local host */
  16. int Cflag;    /* print costs (-c option) */
  17. int Dflag;    /* penalize routes beyond domains (-D option) */
  18. int Iflag;    /* ignore case (-i option) */
  19. int Tflag;    /* trace links (-t option) */
  20. int Vflag;    /* verbose (-v option) */
  21. int Fflag;    /* print cost of first hop */
  22. int Lineno = 1;    /* line number within current input file */
  23. int Argc;    /* external copy of argc (for input files) */
  24. extern void die();
  25. extern int tracelink();
  26.  
  27. /* imports */
  28. extern char *optarg;
  29. extern int optind;
  30. extern long Lcount, Ncount;
  31. extern long allocation();
  32. extern void wasted(), mapit(), hashanalyze(), deadlink();
  33. extern char *local();
  34. extern node *addnode();
  35. extern int getopt(), yyparse();
  36. extern void printit();
  37. extern int Donelast;
  38.  
  39. #define USAGE "usage: %s [-vciDf] [-l localname] [-d deadlink] [-t tracelink] [-g edgeout] [-s treeout] [-a avoid] [files ...]\n"
  40.  
  41. main(argc, argv) 
  42.     register int argc; 
  43.     register char **argv;
  44. {    char *locname = 0, *bang;
  45.     register int c;
  46.     int errflg = 0;
  47.  
  48.     setbuf(stderr, (char *) 0);
  49.     (void) allocation();    /* initialize data space monitoring */
  50.     Cfile = "[deadlinks]";    /* for tracing dead links */
  51.     Argv = argv;
  52.     Argc = argc;
  53.  
  54.     while ((c = getopt(argc, argv, "cd:Dfg:il:s:t:v")) != EOF)
  55.         switch(c) {
  56.         case 'c':    /* print cost info */
  57.             Cflag++;
  58.             break;
  59.         case 'd':    /* dead host or link */
  60.             if ((bang = index(optarg, '!')) != 0) {
  61.                 *bang++ = 0;
  62.                 deadlink(addnode(optarg), addnode(bang));
  63.             } else
  64.                 deadlink(addnode(optarg), (node *) 0);
  65.             break;
  66.         case 'D':    /* penalize routes beyond domains */
  67.             Dflag++;
  68.             break;
  69.         case 'f':    /* print cost of first hop */
  70.             Cflag++;
  71.             Fflag++;
  72.             break;
  73.         case 'g':    /* graph output file */
  74.             Graphout = optarg;
  75.             break;
  76.         case 'i':    /* ignore case */
  77.             Iflag++;
  78.             break;
  79.         case 'l':    /* local name */
  80.             locname = optarg;
  81.             break;
  82.         case 's':    /* show shortest path tree */
  83.             Linkout = optarg;
  84.             break;
  85.         case 't':    /* trace this link */
  86.             if (tracelink(optarg) < 0) {
  87.                 fprintf(stderr, "%s: can trace only %d links\n", Argv[0], NTRACE);
  88.                 exit(1);
  89.             }
  90.             Tflag = 1;
  91.             break;
  92.         case 'v':    /* verbose stderr, mixed blessing */
  93.             Vflag++;
  94.             break;
  95.         default:
  96.             errflg++;
  97.         }
  98.  
  99.     if (errflg) {
  100.         fprintf(stderr, USAGE, Argv[0]);
  101.         exit(1);
  102.     }
  103.     argv += optind;        /* kludge for yywrap() */
  104.  
  105.     if (*argv)
  106.         Donelast = -1;
  107.         /* freopen( NULL_FILE, "r", stdin); */
  108.     else
  109.         Cfile = "[stdin]";
  110.  
  111.     if (!locname) 
  112.         locname = local();
  113.     if (*locname == 0) {
  114.         locname = "lostinspace";
  115.         fprintf(stderr, "%s: using \"%s\" for local name\n",
  116.                 Argv[0], locname);
  117.     }
  118.  
  119.     Home = addnode(locname);    /* add home node */
  120.     Home->n_cost = 0;        /* doesn't cost to get here */
  121.  
  122.     (void) yyparse();            /* read in link info */
  123.  
  124.     if (Vflag > 1)
  125.         hashanalyze();
  126.     vprintf(stderr, "%d nodes, %d links, alloc %ldk\n", 
  127.                 Ncount, Lcount, allocation());
  128.  
  129.     Cfile = "[backlinks]";    /* for tracing back links */
  130.     Lineno = 0;
  131.  
  132.     /* compute shortest path tree */
  133.     mapit();
  134.     vprintf(stderr, "allocation is %ldk after mapping\n", allocation());
  135.  
  136.     /* traverse tree and print paths */
  137.     printit();
  138.     vprintf(stderr, "allocation is %ldk after printing\n", allocation());
  139.  
  140.     wasted();    /* how much was wasted in memory allocation? */
  141.  
  142.     return 0;
  143. }
  144.  
  145. void
  146. die(s)
  147.     char *s;
  148. {
  149. #ifdef DEBUG
  150.     extern int abort();
  151.  
  152.     fprintf(stderr, "%s: %s\n", Argv[0], s);
  153.     fflush(stdout);
  154.     fflush(stderr);
  155.     abort();
  156. #else
  157.     fprintf(stderr, "%s: %s; notify the authorities\n", Argv[0], s);
  158.     exit(-1);
  159. #endif
  160. }
  161.